package com.wc5818.wcBasicInfo.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wc5818.common.AjaxResult;
import com.wc5818.wcBasicInfo.entity.WcBasicInfo;
import com.wc5818.wcBasicInfo.mapper.WcBasicInfoMapper;
import com.wc5818.wcComOrder.entity.WcComOrder;
import com.wc5818.wcComOrder.mapper.WcComOrderMapper;
import com.wc5818.wcSysUser.entity.WcSysUser;
import com.wc5818.wcWorkflowChulr.entity.WcWorkflowChulr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2020-02-22
 */
@RestController
@RequestMapping("/wcBasicInfo")
public class WcBasicInfoController {
    @Autowired
    WcBasicInfoMapper wcBasicInfoMapper;
    @Autowired
    WcComOrderMapper wcComOrderMapper;

    @GetMapping("/getBaseInfoByOrderId")
    public Object getBaseInfoByOrderId(String orderId) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        List list = wcBasicInfoMapper.selectList(new QueryWrapper<WcBasicInfo>().eq("orderId", orderId).orderByDesc("caretdata"));
        if(list != null  && list.size() > 0) {
            ajaxResult.setSuccess(true);
            ajaxResult.setData(list.get(0));
        }

        return ajaxResult;
    }

    @PostMapping("/addBasicInfo")
    public Object addBasicInfo(@RequestBody WcBasicInfo wcBasicInfo) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);

        if (wcBasicInfo.getId() == null) {
            try {
                if (wcBasicInfoMapper.insert(wcBasicInfo) > 0)
                    ajaxResult.setSuccess(true);
            } catch (Exception error) {
                ajaxResult.setErrorMsg(error.getMessage());
            }

        } else {
            if(wcBasicInfoMapper.updateById(wcBasicInfo) >=0 )
                ajaxResult.setSuccess(true);
        }

        return ajaxResult;
    }

    @RequestMapping(value = "/selectBasicInfo", method = RequestMethod.POST, produces = {"application/json;charsetset=UTF-8"})
    public @ResponseBody
    Object selectBasicInfo(@RequestBody WcBasicInfo wcBasicInfo) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        WcBasicInfo wcBasicInfo1 = wcBasicInfoMapper.selectOne(new QueryWrapper<WcBasicInfo>().eq("orderId", wcBasicInfo.getOrderId()));


            ajaxResult.setSuccess(true);
            ajaxResult.setData(wcBasicInfo1);

        return ajaxResult;
    }
}
